<template>
  <div class="center">
    <div class="table_button flex felx_align">
      <el-button type="primary" size="mini" icon="el-icon-refresh" @click="Refresh"></el-button>
      <el-select v-permission="['超级管理员','管理员', '代理商']" v-model="value" placeholder="请选择归属" size="mini" filterable @change="infoChange" style="margin-left:20px">
        <el-option v-for="(item,index) in affiliation" :key="index" :label="`${item.username} - ${item.title}`" :value="item.id"></el-option>
      </el-select>
      <div>
        <el-select v-permission="['超级管理员','管理员', '代理商','学校']" v-model="batchV" placeholder="请选择批次" size="mini" style="margin-left:20px" @change="batchChange">
          <el-option v-for="(item,index1) in batchArray" :key="index1" :label="item.batch" :value="item.id"></el-option>
        </el-select>
        <!-- <el-button icon="el-icon-search" size="mini"></el-button> -->
      </div>
      <div class="table_search flex felx_align" style="margin-left:20px;width:20%">
        <div class="appName">应用</div>
        <el-select v-model="listQuery.app_infoes" placeholder="请选择应用包名" style="width:100%" @change="listChange" size="mini">
          <el-option v-for="(item,index) in AppPackage" :key="index" :label="`${item.app_name} - ${item.app_package}`" :value="item.app_package"></el-option>
        </el-select>
      </div>
    </div>
    <!-- 分类管理table -->
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="username" label="归属" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="app_batch" label="批次" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="app_name" label="应用名称" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="app_version_name" label="版本号" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="sn" label="SN码" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="install_time" label="安装时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.install_time| newDate}}</template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { findPackageSn } from "@/api/supervise";
import { AppPackage } from "@/api/deployment.js";
import { getAscription, getBatchTwo } from "@/api/equipment.js";
import { queryUsernameBatch } from "@/api/appUpdate.js";
import permission from '@/directive/permission/index.js' // 权限判断指令
export default {
  components: { dynamicTable },
  directives: { permission },
  data() {
    return {
      checkboxVal: ["归属", "批次", "应用名称", "版本号", "SN码", "安装时间"],
      key: 1,
      colData: [], //动态控制table
      brandTable: [],
      total: 0, //总数量
      //app包名
      AppPackage: [],
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), app_infoes: undefined, type: 1, sn: undefined },
      searchTable: "",
      affiliation: [],
      batchArray: [],
      value: undefined,
      batchV: undefined
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getAppPackage()
    this.getBatchTwos()
    this.getAscriptions()
  },
  methods: {
    //根据SN码搜索
    searchSn() {
      this.listQuery.sn = this.searchTable
      this.listQuery.type = 2
      this.getfindPackageSn()
    },
    getfindPackageSn() {
      findPackageSn(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
      }).catch(err => {
        this.brandTable = []
        this.total = 0
        console.log('失败', err)
      })
    },
    //下拉选中事件
    listChange(value) {
      this.listQuery.app_infoes = value
      this.listQuery.userId = this.value ? this.value : localStorage.getItem("ID")
      this.listQuery.bId = this.batchV
      this.listQuery.type = 1
      this.getfindPackageSn()
    },
    //根据ID获取包名信息
    getAppPackage(val) {
      let data = { userId: localStorage.getItem("ID"), bId: val }
      AppPackage(data).then(res => {
        this.AppPackage = res.data
      }).catch(err => {
        this.AppPackage = []
      })
    },
    batchChange(val) {
      this.listQuery.app_infoes = undefined
      this.getAppPackage(val)
    },
    infoChange(val) {
      this.batchV = undefined
      this.listQuery.app_infoes = undefined
      this.getBatchTwos(val)
    },
    //设备维护获取归属用户信息
    getAscriptions() {
      getAscription({ userId: localStorage.getItem("ID") }).then(res => {
        //this.snParams = res.data.sort((a, b) => a.username.localeCompare(b.username))
        this.affiliation = res.data.sort((x, y) => {
          let reg = /[a-zA-Z0-9]/
          if (reg.test(x.username) || reg.test(y.username)) {
            if (x.username > y.username) {
              return 1
            } else if (x.username < y.username) {
              return -1
            } else {
              return 0
            }
          } else {
            return x.username.localeCompare(y.username)
          }
        })
      }).catch(err => {
        this.affiliation = []
      })
    },

    //根据ID获取批次列表
    getBatchTwos(id = localStorage.getItem("ID")) {
      let data = { userId: id }
      getBatchTwo(data).then(res => {
        this.batchArray = res.data
      }).catch(err => {
        this.batchArray = []
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getfindPackageSn();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getfindPackageSn();
    },
    Refresh() {
      this.listQuery = {
        startSize: 1,
        pageSize: 10,
        userId: localStorage.getItem("ID"),
        app_infoes: '',
        type: 1,
        sn: undefined
      },
        this.getfindPackageSn()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.searchBox {
  border-radius: 2px;
  margin-bottom: 10px;
  background: #f5f5f5;
  padding: 20px;
}
.appName {
  background-color: #409eff;
  color: #ffffff;
  border: 1px solid #409eff;
  border-radius: 4px;
  padding: 0 20px;
  white-space: nowrap;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  line-height: 28px;
  font-size: 12px;
  border-right: none;
  height: 28px;
  border-radius: 4px 0 0 4px;
}
</style>

<style scoped>
/deep/ .el-input--suffix .el-input__inner {
  border-radius: 0 4px 4px 0;
}
</style>